package com.yuwj.chinaa.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.uuzuche.lib_zxing.activity.CodeUtils;
import com.yuwj.chinaa.R;
import com.yuwj.chinaa.base.BaseActivity;
import com.yuwj.chinaa.entity.EntityAppInfo;
import com.yuwj.chinaa.presenter.IPresenter;
import com.yuwj.chinaa.presenter.presenterImpl.PresenterImpl;
import com.yuwj.chinaa.util.ClipboardUtil;
import com.yuwj.chinaa.util.SharedpreferenceUtil;
import com.yuwj.chinaa.util.ToastUtil;

import org.json.JSONObject;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 我的分享推广界面
 */
public class MePromotionActivity extends BaseActivity {


    @BindView(R.id.ll_copy)
    LinearLayout llCopy;
    @BindView(R.id.iv_qrcode)
    ImageView ivQrcode;
    @BindView(R.id.tv_invite_code)
    TextView tvInviteCode;

    IPresenter presenter;
    Bitmap bitmapWithQRCode;

    public static Intent createIntent(Context context) {
        return new Intent(context, MePromotionActivity.class);
    }

    @Override
    public int setCustomContentViewResourceId() {
        return R.layout.activity_me_promotion;
    }

    @Override
    public void initParam() {
        presenter = new PresenterImpl(this);
    }

    @Override
    public void initView() {
        EntityAppInfo entityAppInfo = SharedpreferenceUtil.getInstance().getEntityAppInfo(context);
        if (entityAppInfo != null && !TextUtils.isEmpty(entityAppInfo.getDownloadurl())) {
            bitmapWithQRCode = CodeUtils.createImage(entityAppInfo.getDownloadurl(), 400, 400, null);
            if (bitmapWithQRCode != null) {
                ivQrcode.setImageBitmap(bitmapWithQRCode);
            } else {
                ToastUtil.getInstance().shortLength(context, "二维码生成失败");
            }
        }

    }

    @Override
    public void initOperator() {
        JSONObject param = new JSONObject();
        presenter.userInvitationCode(param.toString());

    }

    @OnClick({R.id.ll_copy})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_copy:
                String inviteCode = tvInviteCode.getText().toString();
                ClipboardUtil.copyText(context, inviteCode);
                ToastUtil.getInstance().shortLength(context, "复制成功");
                break;
        }
    }

    @Override
    public void onUserInvitationCodeSuccess(String invitationCode) {
        super.onUserInvitationCodeSuccess(invitationCode);

        tvInviteCode.setText(invitationCode);

    }

    @Override
    public void onUserInvitationCodeFail(Exception e) {
        super.onUserInvitationCodeFail(e);
        ToastUtil.getInstance().shortLength(context, e.getMessage() + "");
    }
}
